<template>
          <!-- 筛选区 -->
   <div class="sub-filter" v-loading="filterLoading">
     <div class="item">
       <div class="head">品牌：</div>
       <div class="body">
         <a href="javascript:;" @click="changeBrand(item.id)" :class="{active: item.id === filterData.selectedBrand}" v-for="item in filterData.brands" :key="item.id">{{item.name}}</a>
       </div>
     </div>
     <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
       <div class="head">{{item.name}}:</div>
       <div class="body">
         <a href="javascript:;" @click="changeProp(item,prop.id)" :class="{active: prop.id === item.selectedProp}" v-for="prop in item.properties" :key="prop.id">{{prop.name}}</a>
         <!-- <a href="javascript:;" v-for="item in filterDate.brands" :key="item.id">{{item.name}}</a> -->
       </div>
     </div>
   </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '../../../api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    // 监听二级类目ID的变化获取筛选数据
    const filterData = ref({})
    const filterLoading = ref(false)
    watch(() => route.params.id, (newValue) => {
      // 变化后的ID有值 且 处于二级类名路由下
      if (newValue && `/category/sub/${newValue}` === route.path) {
        filterLoading.value = true
        // 发请求拿数据
        findSubCategoryFilter(route.params.id).then(data => {
          // 每一组筛选条件都缺少 全部   要加上
          // console.log(data.result)
          data.result.selectedBrand = null
          data.result.brands.unshift({ id: null, name: '全部' })
          data.result.saleProperties.forEach(item => {
            // console.log(item)
            // 初始索引为null 全部高亮
            item.selectedProp = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          filterData.value = data.result
          filterLoading.value = false
          // console.log(filterDate.value.brands)
        })
      }
    }, { immediate: true })
    // console.log(filterDate.brands)
    // 获取筛选参数的函数
    const getFilterParams = () => {
      const obj = { brandId: null, attrs: [] }
      // 品牌
      obj.brandId = filterData.value.selectedBrand
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        // 我们如果直选中一个的话 判断一个
        if (item.selectedProp) {
          const prop = item.properties.find(prop => prop.id === item.selectedProp)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      // 参考数据：{brandId:'',attrs:[{groupName:'',propertyName:''},....]}
      return obj
    }
    // 记录当前选择的品牌
    const changeBrand = (brandId) => {
      // 点击同一个的话 要阻止
      if (filterData.value.selectedBrand === brandId) return
      filterData.value.selectedBrand = brandId
      // 发请求到父组件
      emit('filter-change', getFilterParams())
    }
    // 纪录当前选择的品牌的属性
    const changeProp = (item, propId) => {
      // console.log(item, propId)
      if (item.selectedProp === propId) return
      item.selectedProp = propId
      emit('filter-change', getFilterParams())
    }
    return { filterData, filterLoading, changeBrand, changeProp }
  }
}
</script>
<style scoped lang='less'>
@import '../../../assets/styles/variable.less';
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      font-size: 13px;
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @c;
          }
        }
      }
    }
  }
</style>
